﻿using System;
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneMgr : Singleton<SceneMgr>
{
    public static void OnEnterMap(Cmd cmd)
    {
        if (!Nets.CheckCmd(cmd, typeof(EnterMapCmd))) return;
        EnterMapCmd enterMapCmd = cmd as EnterMapCmd;

        UIManager.instance.RemoveLayer();

        SceneMgr.instance.loadScene(enterMapCmd.MapID);
    }

    private void loadScene(int mapID)
    {
        var mapDatabase = MapTable.instance[mapID];
        if (mapDatabase == null)
        {
            Debug.LogError("未找到地图 ： " + mapID);
            return;
        }
        //阻塞消息，暂时不处理
        Nets.instance.Pause = true;
        // 同步
        //SceneManager.LoadScene(mapDatabase.ScenePath);
        //异步
        var ao = SceneManager.LoadSceneAsync(mapDatabase.ScenePath);
        //QuickCoroutine.instance.StartCoroutine1(loadEnd(ao));

        //QuickCoroutine.Instance.WaitTime(10, false, OnWaitCoroutine1(ao));
        Debug.Log("场景加载中");
        //TimerMgr.instance.CreateTimer(5,1,loadEnd1);
        FightUIMgr.instance.Init();
    }

    private void loadEnd1()
    {
        FightUIMgr.instance.Init();
    }

    private IEnumerator loadEnd(AsyncOperation ao)
    {
        Debug.Log("场景加载中~~~~");
        while (ao.isDone)
        {
            //ao.progress
            yield return new WaitForEndOfFrame();
        }
        Debug.Log("场景加载完毕");
        Nets.instance.Pause = false;

        FightUIMgr.instance.Init();

    }
}
